package geek;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sam
 * 2021/10/20 22:02
 * @description
 * @editor
 * @description
 *
 *
 * 异位词
 */
public class Lesson14 {



    @Test
    public void mapEqual(){
        Map<String,String> a = new HashMap<>();
        a.put("a","a");
        Map<String,String> b = new HashMap<>();
        b.put("a","a");

        Assertions.assertTrue(validAnagram("abc","acb"));
        Assertions.assertFalse(validAnagram("abcd","acb"));
    }

    /*
    sort
     */
    private boolean validAnagram(String a,String b){
        char[] aChar = a.toCharArray();
        char[] bChar = b.toCharArray();
        if (aChar.length!=bChar.length)
            return false;
        Arrays.sort(aChar);
        Arrays.sort(bChar);
        return String.valueOf(aChar).equals(String.valueOf(bChar));
    }

    /*
    map
     */
    private boolean validAnagramMap(String a,String b){
        char[] aChar = a.toCharArray();
        char[] bChar = b.toCharArray();
        if (aChar.length!=bChar.length)
            return false;
        Arrays.sort(aChar);
        Arrays.sort(bChar);
        return String.valueOf(aChar).equals(String.valueOf(bChar));
    }

    private boolean validAnagramArray(String a,String b){
        char[] aChar = a.toCharArray();
        char[] bChar = b.toCharArray();
        if (aChar.length!=bChar.length)
            return false;
        Arrays.sort(aChar);
        Arrays.sort(bChar);
        return String.valueOf(aChar).equals(String.valueOf(bChar));
    }


}
